var jsHighLighter = {
  doFormatHtml: function(docHtml, fragments){
		var posX = 0;
		var posY = 0;
		var html = "";
		if(fragments == null || fragments.length == 0){
			debug("[highlighter.js::doFormatHtml]************************** highLightHtml **************************",4);
			//debug("[highlighter.js::doFormatHtml]\n" + docHtml,4);
			//TAG = "<hypertopic type=\"end\" id=\"ht_fragment_end_0_0\"></hypertopic>";
			this.writeToPage(docHtml+HIGHLIGHT_TAG);
		  var docContent = mainWindow.getBrowser().contentDocument.documentElement.innerHTML;

      //debug("[highlighter.js::doFormatHtml]************************** highLightHtml **************************",4);
      //debug("[highlighter.js::doFormatHtml]\n" + docContent,4);
			//debug("[highlighter.js::doFormatHtml]************************** highLightHtml **************************",4);

      sidebar.docHtml = docContent;
			return false;
		}
    debug("[highlighter.js::doFormatHtml] Begin function:", 4);
		var offsetLength = 0;
		for(var i=0; i < fragments.length; i++){
			var highlight = {};
			var frag = fragments[i];
			highlight.startPos = frag.startPos;
			highlight.endPos = frag.endPos;
			highlight.colorIndex = frag.colorIndex;
			highlight.content = frag.content;
			highlight.originalLength = frag.endPos-frag.startPos;
			highlight.originalHtml = docHtml.substr(highlight.startPos, highlight.originalLength);
			highlight.highLightedHtml = jsSelection.highLightByIndexOf(highlight.originalHtml, highlight.colorIndex);
			highlight.highLightedHtml = jsSelection.highLightByIndexOf(highlight.originalHtml, highlight.colorIndex);
			highlight.highLightedHtml = "<hypertopic type=\"start\" id=\"ht_fragment_" + highlight.startPos + "_" + highlight.endPos + "\"></hypertopic>" + highlight.highLightedHtml
															  + "<hypertopic type=\"end\" id=\"ht_fragment_end_" + highlight.startPos + "_" + highlight.endPos + "\"></hypertopic>";

			posY = highlight.startPos;
			html += docHtml.substring(posX,posY) + highlight.highLightedHtml;
			posX = highlight.endPos;
			//var_dump("[highlighter.jsdoFormatHtml]", highlight, 4);
		}
		html += docHtml.substring(posX);
		html = html + HIGHLIGHT_TAG;
		//var_dump("[highlighter.jsdoFormatHtml]", html, 4);
		this.writeToPage(html);
  },

  writeToPage: function(htmlSource){
    var doc = mainWindow.getBrowser().contentDocument.wrappedJSObject;
    doc.open("text/html", "replace");
    doc.write(htmlSource);
    doc.close();
  },
  padZero:function(num){
    var str = num + "";
    var len = str.length;
    if(len  < 10)
      for(var i=0; i<(10-len ); i++)
        str = "0" + str;
    return str;
  }
};